# coding=utf-8
from flask import Flask, request
import xmltodict
from utils.notoken.cReplyMsg import ReplyMsgDemo
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
app = Flask(__name__)


@app.route('/notoken/', methods=['GET', 'POST'])
def wechat_validate():
    rmd = ReplyMsgDemo()
    if request.method == 'GET':
        return rmd.check_status(request)
    elif request.method == 'POST':
        xml = request.data
        req = xmltodict.parse(xml)['xml']
        if req.get('Content') != 'test':
            req['r_code'] = 200
            return rmd.reply_msg(request=req)
        else:
            req['r_code'] = 333
            req['Content'] = 'success'
            return rmd.reply_msg(request=req)


def _test_accesstoken_none():
    """
    _test_accesstoken_none()
    >
    648e4cc1.ngrok.io
    """
    app.run(debug=True, host='0.0.0.0', port=8000)


if __name__ == '__main__':
    _test_accesstoken_none()
